package com.hdy.orderforthree.mvp;

import com.hdy.net.order.NetCallback;
import com.hdy.net.order.bean.BasicDataResponse;

public class FoodPresenter implements IFoodContract.Presenter {

    private FoodView mFoodView;
    private FoodModel mFoodModel;

    public FoodPresenter(FoodView view, FoodModel model){
        mFoodView = view;
        mFoodModel = model;
    }

    public void setUpdateViewCallback(TitleBarPresenter.UpdateViewCallback cb) {
        mFoodView.setUpdateViewCallback(cb);
    }

    @Override
    public void loadFoodData() {
        mFoodView.showLoading();

        NetCallback callback = new NetCallback<BasicDataResponse>(){
            @Override
            public void onError(Throwable e) {
                mFoodModel.getFoodData(this);
            }

            @Override
            public void onStart() {
            }

            @Override
            public void onCompleted() {
            }

            @Override
            public void onNext(BasicDataResponse basicDataResponse) {
                mFoodView.showData(basicDataResponse);
            }
        };
        mFoodModel.getFoodData(callback);
    }

    @Override
    public void detachView() {
        this.mFoodView = null;
    }

    @Override
    public boolean isViewAttached(){
        return mFoodView != null;
    }

    public void notifyDataSetChanged() {
        mFoodView.notifyDataSetChanged();
    }
}
